|
|
|
|
|
|
|
Why Use INSERT Statements? |
|
|
|
|
|
|
|
|
Even though you most often perform data entry using Visual Basiccoded data-entry screens tied to Visual Basic data controls, there are times when using the INSERT statement can prove more efficient. An excellent example of using INSERT statements is the installation of a new database system. Often, several data tables need to be populated with default values before people can start using a system. You can use the INSERT statement to perform the initial data load. |
|
|
|
|
|
|
|
|
Another use for the INSERT statement is in converting data from one database to another. Often, you can use INSERT statements to load existing data in one format into your newly designed relational database. |
|
|
|
|
|
|
|
|
Finally, you can use INSERT statements to quickly add data to tables that would be too tedious to enter using data-entry screens. |
|
|
|
|
|
|
|
|
Using the INSERT INTO Statement |
|
|
|
|
|
|
|
|
The basic form of the INSERT statement is |
|
|
|
|
|
|
|
|
INSERT INTO TableName(field1, field2) VALUES (value1, value2); |
|
|
|
|
| |
|
|
|
|
INSERT and INSERT INTO statements are often used interchangeably. For the most part, this book uses the latter term. |
|
|
|
|
|
|
|
|
|
|
The INSERT SQL statement has three parts. The TableName identifies the table that you want to update. The (field1, field2) part of the statement identifies the columns into which you add data. The (value1, value2) part of the statement identifies the exact values you add to the fields you identified. You can name as few or as many fields as you like in the field portion of the statement. However, you must supply a list of values that has the same number of values and the same data type as those identified in the field portion of the statement. Also, you must list the values in the same order as the fields. The first value is placed in the first field, the second value in the second field, and so on. |
|
|
|
|
|
|
|
|
Let's use SQL-VB5 to create a working example of the INSERT statement. Open a new .SQV script called SQLVB05.SQV using the File | New command from the main menu. Enter the script shown in Listing 15.1, save it, and execute it using the File | Run menu option. Refer to Figure 15.1 to compare your results. |
|
|
|
|
|